
<template>
    <div class="ingr-basic-comp">
        <div class="title-box">
            <TitleBox>
                <template #titlecont>
                    <span class="label-link-txt" v-lang="{lang: controlSto.language, lgid: -116}">Ingredient</span>
                    <!-- <span v-lang="{lang: controlSto.language, lgid: -24}">Information</span> -->
                </template>
            </TitleBox>
        </div>

        <div class="condition-box">
            <div class="condition-right">
                <!-- <div class="ipt-cont">
                    <el-input class="ipt-search" v-model="condObj.searchword" autofocus :placeholder="searchPlace" />
                </div>
                <SearchBtn class="search-item" @click.stop="toSearch"/> -->
                <SearchLoad type='ingredientBasic' :uiCode="condObj.uiCode" :para="{
                    'ingrId': detailId,
                    'langCode': controlSto.language == 1 ? 'ENG' : 'CHN',
                    'uiTypeCode': condObj.uiCode
                }"/>
            </div>
        </div>



        <div class="info-cont-box">
            <DetailItemList :data="dataObj.dataList"></DetailItemList>
        </div>
    </div>
</template>

<script setup>
    import { ref, reactive, toRefs, onBeforeMount, computed, watch } from 'vue'
    import TitleBox from '@/components/TitleBox.vue'
    import SearchLoad from '@/components/SearchLoad.vue'
    import { langTxt } from '@/utils/util'
    import { controlStore } from '@/stores/control'
    import { debounce } from 'lodash'

    import { getIngredientBasic } from '@/service/tcm'

    const emitter = defineEmits(['toCalc']);
    const props = defineProps({
        detailId: {
            type: [Number, String],
            default: ''
        }
    });
    const { detailId } = toRefs(props);


    let searchDebounce = null;

	const controlSto = controlStore();
    const isLoading = ref(true);

    const condObj = reactive({
		searchword: '',
        word: '',
		filterKey: 'ingr2',
        uiCode: 'ingredient_basic_info',
        total: 0,
        pageSize: 10,
        pageIndex: 1,
        // prescription_basic_info
	});
	const dataObj = reactive({
		dataList: []
	});

    onBeforeMount(()=> {
        initDebounce();
        toGetData();
    });

    const searchPlace = computed(() => {
		return langTxt(controlSto.language, '-15');
	});

    const initDebounce = () => {
        searchDebounce = debounce((e) => {
           getDataList(e);
        }, 400);
    };

    
    const toGetData = () => {
        searchDebounce && searchDebounce();
    };
    const getDataList = () => {
        let params = {
            langCode: controlSto.language == 1 ? 'ENG' : 'CHN',
            ingrId: detailId.value,
            // content: condObj.word,
            uiTypeCode: condObj.uiCode,
			// pageNo: condObj.pageIndex || 10,
			// pageSize: condObj.pageSize || 1
        };
        isLoading.value = true;
        getIngredientBasic(params).then((res) => {
            isLoading.value = false;
            if (res.success && res.data) {
                dataObj.dataList = res.data || [];
                condObj.total = res.data.totalCount || 0;
            }
        }).catch(() => {
            isLoading.value = false;
        });

        emitter('toCalc');
    };

    const chgSize = (e) => {
        condObj.pageSize = e || 10;
        toGetData();
    };
    const chgCurrnt = (e) => {
        condObj.pageIndex = e || 1;
        toGetData();
    };

    const toSearch = () => {
        condObj.word = condObj.searchword;
        condObj.pageIndex = 1;
        toGetData();
    };
    watch(() => controlSto.language, (n, v) => {
        if(n != v) {
            toGetData();
        }
    }, { immediate: true });

    
</script>


<style lang="scss" scoped>
    .ingr-basic-comp {
        .condition-box {
            display: flex;
            padding: 24px 0 0;
            justify-content: space-between;
            position: relative;
            .condition-right {
                position: absolute;
                right: 0px;
                top: -45px;
                .ipt-cont {
                    display: inline-block;
                    padding-right: 16px;
                    .ipt-search {
                        // width: 300px;
                    }
                }
                .search-item {
                    margin-right: 16px;
                }
            }
        }
        .page-box {
            padding: 10px 0;
        }
    }
</style>
